package cn.bocweb.spinning.view.forun;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForunGetCatBean;

/**
 * Created by chenchangxing on 16/4/6.
 */
public class ForunSelectCateAdapter extends RecyclerView.Adapter {

    List<ForunGetCatBean.DataBean> list;
    OnItemClickListener onItemClickListener;
    Context context;

    public void setSelected(int selected) {
        this.selected = selected;
    }

    int selected;

    public interface OnItemClickListener {
        public void onItemClick(int position, String id);
    }

    public ForunSelectCateAdapter(List<ForunGetCatBean.DataBean> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_set_ques_warehouse, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        holder1.quesName.setText(list.get(position).getTitle());

        holder1.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(position, list.get(position).getId());
            }
        });

        if (selected == position) {
            holder1.btnWarehouse.setCardBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
        } else {
            holder1.btnWarehouse.setCardBackgroundColor(context.getResources().getColor(R.color.bg_set_ques));
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ques_name)
        TextView quesName;
        @Bind(R.id.bg_warehouse)
        LinearLayout bgWarehouse;
        @Bind(R.id.btn_warehouse)
        CardView btnWarehouse;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
